using CfgTable;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using IQIGame.Onigao.Framework;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public partial class UIMapManager
    {
        private class UIFogMapSymbol : UIBaseMapSymbol
        {
            protected override void BeforeInit()
            {

            }

            protected override void OnInit()
            {

            }

            protected override void OnShow()
            {

            }

            protected override void OnHide()
            {

            }

            public override void Reset()
            {
                if (_fog != null && _maskTarget != null)
                {
                    PlayFadeEffect(_fog).Forget();
                    _maskTarget = null;
                    _fog = null;
                    if (_rectMask != null)
                    {
                        _rectMask.RemoveWeakSoftnessMaskTarget(_maskTarget);
                        _rectMask = null;
                    }
                }

                base.Reset();
            }

            private async UniTaskVoid PlayFadeEffect(GameObject fog)
            {
                var layerRoot = owner.GetLayer((int)StaticLayerType.FogLayer).root;
                if (layerRoot == null)
                {
                    Object.Destroy(fog);
                    return;
                }
                var ump = fog.GetComponent<UIMaterialParamter>();
                fog.transform.SetParent(layerRoot.transform);
                var tweener = DOTween.To(() => ump.fValue, fv => ump.fValue = fv,
                    1f, 3f);
                await tweener.SetTarget(ump).SetLink(fog).GetAwaiter();
                Object.Destroy(fog);
            }

            protected override void Draw()
            {
                SetFog().Forget();
            }

            private async UniTaskVoid SetFog()
            {
                if (_fog == null)
                {
                    var cid = data.config.Id;
                    var prefab = await resLoader.LoadABAssetAwait<GameObject>(GetPrefabPath());
                    if (data.config.Id != cid)
                    {
                        return;
                    }

                    _fog = Object.Instantiate(prefab, transform);
                    _fog.transform.localPosition = Vector3.zero;
                    _maskTarget = _fog.GetComponent<ExRawImage>();

                    transform.SetParent(transform.parent.GetChild(data.config.Id % 10000 - 1));
                    transform.localPosition = Vector3.zero;
                }

                //处理迷雾和地图需要不用级别的softness的问题
                //tip: 目前迷雾因为有单独的材质,已经在着色器里处理了，所以不用在ui代码里处理
                /*
                var rectMask = MaskUtilities.GetRectMaskForClippable(_maskTarget);
                if (rectMask is ExRectMask2D exRectMask2D)
                {
                    exRectMask2D.weakFactor = 0f;
                    exRectMask2D.AddWeakSoftnessMaskTarget(_maskTarget);
                    _rectMask = exRectMask2D;
                }*/
            }

            private string GetPrefabPath()
            {
                var cfgRegion = TableCenter.regionInstance.Get(owner.regionCid);
                return cfgRegion.FogPath_fullPath + data.config.Prefab;
            }

            private GameObject _fog;
            private ExRawImage _maskTarget;
            private ExRectMask2D _rectMask;
            private static readonly int _fogFactorId = Shader.PropertyToID("_FogFactor");
        }
    }
}